What is contentful-sdk-core?
The contentful-sdk-core npm package provides core functionalities for interacting with the Contentful Content Delivery API. It is designed to be a foundational library that other Contentful SDKs can build upon, offering utilities for making HTTP requests, handling responses, and managing errors.
What are contentful-sdk-core's main functionalities?
HTTP Client
This feature allows you to create an HTTP client configured to interact with the Contentful API. The code sample demonstrates how to create an HTTP client using Axios and make a GET request to fetch entries.
const { createHttpClient } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id'
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => console.error(error));
Error Handling
This feature provides a standardized way to handle errors that occur during API requests. The code sample shows how to use the errorHandler function to manage errors from a GET request.
const { createHttpClient, errorHandler } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id'
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => errorHandler(error));
Request Configuration
This feature allows you to configure various aspects of the HTTP requests, such as headers and query parameters. The code sample demonstrates how to add custom headers to the HTTP client.
const { createHttpClient } = require('contentful-sdk-core');
const axios = require('axios');
const httpClient = createHttpClient(axios, {
accessToken: 'your_access_token',
space: 'your_space_id',
headers: {
'X-Custom-Header': 'customValue'
}
});
httpClient.get('/entries')
.then(response => console.log(response.data))
.catch(error => console.error(error));
Other packages similar to contentful-sdk-core
contentful
The 'contentful' package is a higher-level SDK that builds on top of contentful-sdk-core. It provides more abstracted and user-friendly methods for interacting with the Contentful API, such as fetching entries, assets, and content types. It is more feature-rich and easier to use for common tasks compared to contentful-sdk-core.
contentful-management
The 'contentful-management' package is another higher-level SDK that focuses on the Contentful Management API. It allows you to manage content types, entries, assets, and more. Like the 'contentful' package, it builds on top of contentful-sdk-core but is tailored for content management tasks rather than content delivery.
axios
While 'axios' is a general-purpose HTTP client and not specific to Contentful, it is often used in conjunction with contentful-sdk-core to make HTTP requests. It provides a flexible and powerful API for making HTTP requests, handling responses, and managing errors, similar to the core functionalities provided by contentful-sdk-core.
contentful-sdk-core
This package contains some core modules and utilities used by both the contentful.js and contentful-management.js SDKs.
Vendored axios
The vendor-browser
and vendor-node
directories contain a vendored build of axios which are used respectively on the standalone browser build and on the published npm package.
Axios is vendored because it expects a native or polyfilled implementation of promises. In this particular case, we vendor axios using babel, which uses the babel-plugin-transform-runtime to transform any usage of promises to requires to babel-runtime/core-js/promise
.
Axios can be vendored with npm run vendor:browser
and npm run vendor:node
.
The browser vendored version runs on top of the standalone Axios browser version which is already optimized for this use case (it's not a good idea to try and run babel on top of the normal axios commonjs package as it produces an unnecessarily large file)
Because of this, the follow-redirects dependency of axios needs to be a dependency on this package as well, otherwise it won't be installed.
The additional npm run vendor:version
task is unrelated to axios vendoring and is used to build the library version into the code, to avoid having to bundle code for reading a json file in the browser build.
Future incompatibility between lodash and core-js
According to this issue in the future lodash and core-js will be incompatible.
This means one of two things:
- this package will have to stay on a certain version of lodash
- stop vendoring axios and remove babel-runtime, along with support for node.js 0.10
Given that by the time lodash becomes incompatible with core-js node.js 0.10 will already be officially unsupported, the second approach might be the correct one.